home *** CD-ROM | disk | FTP | other *** search
- // OLEShellDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "OLEShell.h"
- #include "OLEShellDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // COLEShellDlg dialog
-
- COLEShellDlg::COLEShellDlg(CWnd* pParent /*=NULL*/)
- : CDialog(COLEShellDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(COLEShellDlg)
- m_fY = 0.0f;
- m_fX = 0.0f;
- m_sOutput = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void COLEShellDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(COLEShellDlg)
- DDX_Text(pDX, IDC_EDIT2, m_fY);
- DDX_Text(pDX, IDC_EDIT1, m_fX);
- DDX_Text(pDX, IDC_output, m_sOutput);
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(COLEShellDlg, CDialog)
- //{{AFX_MSG_MAP(COLEShellDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnInitOLE)
- ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
- ON_EN_KILLFOCUS(IDC_EDIT2, OnKillfocusEdit2)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_WM_CLOSE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // COLEShellDlg message handlers
-
- BOOL COLEShellDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- m_bBreadboardOpen = FALSE;
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void COLEShellDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void COLEShellDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR COLEShellDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- void COLEShellDlg::OnInitOLE()
- {
- COLEShellApp *myApp = (COLEShellApp *)AfxGetApp();
- if (!m_nsApp.CreateDispatch(_T("NeuroSolutions.Application"))) {
- AfxMessageBox("Cannot open NeuroSolutions application");
- return;
- }
- m_nsApp.restore();
- CString relativePath = m_nsApp.pathFromNS("OLE\\Breadboard\\MLPXor.nsb");
- m_nsObject.AttachDispatch(m_nsApp.open(relativePath));
- m_nsObject.maximize();
- m_bBreadboardOpen = TRUE;
- }
-
- void COLEShellDlg::perform(LPCSTR aString)
- {
- m_nsObject.send(aString);
- }
-
- int COLEShellDlg::performReturningInt(LPCSTR aString)
- {
- VARIANT var = m_nsObject.send(aString);
- ASSERT(V_VT(&var) == VT_I4);
- return var.lVal;
- }
-
- void COLEShellDlg::performSendingFloatArray(float *data, int rows, int cols, LPCSTR aString)
- {
- VARIANT vaResult;
- VariantInit(&vaResult);
- SAFEARRAY FAR* psa;
- SAFEARRAYBOUND sabound[2];
- sabound[0].cElements = rows;
- sabound[0].lLbound = 0;
- sabound[1].cElements = cols;
- sabound[1].lLbound = 0;
- psa = SafeArrayCreate(VT_VARIANT, 2, sabound);
- long n[2];
- for (n[0]=0; n[0]<rows; n[0]++)
- for (n[1]=0; n[1]<cols; n[1]++) {
- VARIANT vItem;
- VariantInit(&vItem);
- vItem.vt = VT_R4;
- vItem.fltVal = *(data + (n[1]*rows) + n[0]);
- if (SafeArrayPutElement(psa, n, &vItem) != S_OK)
- AfxMessageBox("Error: could not write data to variant");
- }
- V_VT(&vaResult) = VT_ARRAY | VT_VARIANT;
- V_ARRAY(&vaResult) = psa;
- m_nsObject.sendDataToEngine(&vaResult, aString);
- }
-
- float COLEShellDlg::performReturningFloat(LPCSTR aString)
- {
- VARIANT var = m_nsObject.send(aString);
- ASSERT(V_VT(&var) == VT_R4);
- return var.fltVal;
- }
-
- float *COLEShellDlg::performReturningFloatArray(LPCSTR aString, int *rows, int *cols)
- {
- VARIANT var = m_nsObject.send(aString);
- ASSERT(V_VT(&var) == (VT_ARRAY | VT_R4));
- LPSAFEARRAY psa = V_ARRAY(&var);
- int iDims = SafeArrayGetDim(psa);
- long lRowLBound, lRowUBound, lColLBound, lColUBound;
- SafeArrayGetLBound(psa,1,&lColLBound);
- SafeArrayGetUBound(psa,1,&lColUBound);
- SafeArrayGetLBound(psa,2,&lRowLBound);
- SafeArrayGetUBound(psa,2,&lRowUBound);
- *rows = (int)lRowUBound-(int)lRowLBound+1, *cols = (int)lColUBound-(int)lColLBound+1;
- float *aBuffer = (float *)malloc(*rows * *cols * sizeof(float));
- long n[2], i;
- i = 0;
- for (n[1]=lRowLBound; n[1]<=lRowUBound; n[1]++) {
- for (n[0]=lColLBound; n[0]<=lColUBound; n[0]++) {
- float fItem;
- if (SafeArrayGetElement(psa, n, &fItem) == S_OK)
- aBuffer[i++] = fItem;
- else
- AfxMessageBox("Error: could not read variant array.");
- }
- }
- return aBuffer;
- }
-
- BOOL COLEShellDlg::performReturningBOOL(LPCSTR aString)
- {
- VARIANT var = m_nsObject.send(aString);
- ASSERT((V_VT(&var) == VT_I4) || (V_VT(&var) == VT_I2));
- return (BOOL)((V_VT(&var) == VT_I4) ? var.lVal : var.iVal);
- }
-
- CString COLEShellDlg::performReturningString(LPCSTR aString)
- {
- VARIANT var = m_nsObject.send(aString);
- ASSERT(V_VT(&var) == VT_BSTR);
- CString tempString = var.bstrVal;
- SysFreeString(var.bstrVal);
- return tempString;
- }
-
- void COLEShellDlg::OnKillfocusEdit1()
- {
- UpdateData(TRUE);
- }
-
- void COLEShellDlg::OnKillfocusEdit2()
- {
- UpdateData(TRUE);
- }
-
- void COLEShellDlg::OnButton2()
- {
- if (!m_bBreadboardOpen)
- AfxMessageBox("You must open the breadboard first.");
- else {
- int inputRows = 2;
- int inputCols = 1;
- float *data = (float *)malloc(inputRows * inputCols * sizeof(float));
- *data = m_fX;
- *(data +1) = m_fY;
- performSendingFloatArray(data, inputRows, inputCols, "oLEInput");
- perform("control.stepExemplar()");
- int outputRows = 1;
- int outputCols = 1;
- float *results = performReturningFloatArray("matrixViewer. getProbeData()", &outputRows, &outputCols);
- if (results) {
- m_sOutput.Format("%f",*results);
- free(results);
- }
- }
- UpdateData(FALSE);
- }
-
- void COLEShellDlg::OnClose()
- {
- m_nsObject.DetachDispatch();
- m_nsApp.DetachDispatch();
- m_nsObject.ReleaseDispatch();
- m_nsApp.ReleaseDispatch();
- CDialog::OnClose();
- }
-